package com.es.esdemo.service;

import com.alibaba.fastjson.JSONArray;
import com.es.esdemo.dao.CmsDao;
import com.es.esdemo.entity.Cms;
import com.es.esdemo.entity.CmsDoc;
import com.es.esdemo.tool.HighLevelClientUtils;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description
 * @author yanghanwei[yanghanwei@bjpp.com]
 * @date 19:48 2020-11-30
 */
@Service
@Slf4j
public class CmsService {

    private String indexName = "cms_index";

    @Resource
    private CmsDao cmsDao;

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    /**
     * mysql getList
     * @return
     */
    public List<Cms> getList(){
        return cmsDao.getList();
    }

    /**
     * 创建索引
     * @return
     */
    public Boolean createIndex() {
        if(HighLevelClientUtils.indexExists(restHighLevelClient, indexName)){
            log.info("createIndex:{} 已存在", indexName);
        }
        boolean index = HighLevelClientUtils.createIndex(restHighLevelClient, indexName, CmsDoc.buildSetting());
        if(!index) {
            log.info("createIndex:{} 失败", indexName);
            return false;
        }
        XContentBuilder mapping = CmsDoc.buildIndexMapping();
        if (mapping != null) {
            return HighLevelClientUtils.createIndexMapping(restHighLevelClient, indexName, mapping);
        }
        return false;
    }

    /**
     * 删除索引
     * @return
     */
    public Boolean deleteIndex() {
        if(!HighLevelClientUtils.indexExists(restHighLevelClient, indexName)){
            log.info("createIndex:{} 不存在", indexName);
            return true;
        }
        return HighLevelClientUtils.deleteIndex(restHighLevelClient, indexName);
    }

    /**
     * 构建数据
     * @return
     */
    public Integer rebuildIndex() {
        List<Cms> list = cmsDao.getList();
        if (list != null && list.size() > 0) {
            HighLevelClientUtils.updateDocs(restHighLevelClient, indexName, JSONArray.toJSONString(list), CmsDoc.ID);
            return list.size();
        }
        return 0;
    }

    /**
     * ES 查询
     * @param pageNo
     * @param pageSize
     * @param keyword
     * @return
     */
    public Object searchList(Integer pageNo, Integer pageSize, Integer id, String keyword) {
        int startNum = (pageNo - 1) * pageSize;
        SearchSourceBuilder searchBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();

        if(id != null){
            boolQuery.should(QueryBuilders.termQuery(CmsDoc.ID, keyword));
        }
        if(!StringUtils.isEmpty(keyword)){
            boolQuery.should(QueryBuilders.matchQuery(CmsDoc.TITLE, keyword).analyzer(CmsDoc.IK));
            boolQuery.should(QueryBuilders.matchQuery(CmsDoc.TAGS, keyword).analyzer(CmsDoc.COMMA));
            boolQuery.should(QueryBuilders.matchQuery(CmsDoc.CONTENT, keyword).analyzer(CmsDoc.IK));
        }
        searchBuilder.query(boolQuery);
        searchBuilder.from(startNum);
        searchBuilder.size(pageSize);
        searchBuilder.sort(CmsDoc.ID, SortOrder.DESC);
        return HighLevelClientUtils.search(restHighLevelClient, indexName, searchBuilder);
    }
}
